<?php

App::uses('AppController', 'Controller');

class CameraController extends AppController {

    /**
     * set layout
     */
    public function beforeFilter() {
        parent::beforeFilter();
        $this->loadModel('Camera');
        $this->set('title_for_layout', "camera");
        $this->menu_active = 'camera';
    }

    /**
     * 
     */
    public function index() {
        $cameras = $this->Camera->getAllCamera($this->request->data);
        //echo "<pre>";var_dump($cameras);die();
        $this->set('cameras', $cameras);
    }

    /**
     * 
     */
    public function add() {
        if ($this->request->data) {
            $this->Camera->create();
            $this->request->data['Camera']['CrtDt'] = date('Y-m-d H:i:s');
            $this->request->data['Camera']['Resolution'] = $this->request->data['Camera']['ResolutionX'] . "x" . $this->request->data['Camera']['ResolutionY'];
            if(strlen($this->request->data['Camera']['Resolution']) > 0){
                $this->request->data['Camera']['Resolution'] = "N".$this->request->data['Camera']['ResolutionX'] . "x" . $this->request->data['Camera']['ResolutionY'];
            }
            if ($this->Camera->save($this->request->data)) {
                $this->Camera->setFrame($this->request->data);
                $this->Session->setFlash(__('Your Camera has been saved.'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to add your Camera.'));
        }else{
            $this->request->data['Camera']['duration'] = 1;
            $this->request->data['Camera']['FrameNo'] = 30;
        }
    }

    /**
     * 
     */
    public function edit($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid Camera'));
        }

        $Camera = $this->Camera->find('first', array('conditions' => array('Camera.Id' => $id)));
        if (!$Camera) {
            throw new NotFoundException(__('Invalid Camera'));
        }

        if ($this->request->is(array('post', 'put'))) {
            $this->Camera->id = $id;
            $this->request->data['Camera']['Resolution'] = $this->request->data['Camera']['ResolutionX'] . " x " . $this->request->data['Camera']['ResolutionY'];
            if(strlen($this->request->data['Camera']['Resolution']) > 0){
                $this->request->data['Camera']['Resolution'] = "N".$this->request->data['Camera']['ResolutionX'] . "x" . $this->request->data['Camera']['ResolutionY'];
            }
            if ($this->Camera->save($this->request->data)) {
                $this->Camera->setFrame($this->request->data);
                $this->Session->setFlash(__('Your Camera has been updated.'));
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash(__('Unable to update your Camera.'));
        }

        if (!$this->request->data) {
            $resolutionArr = split('x', trim($Camera['Camera']['Resolution'],'N'));
            if (isset($resolutionArr[0]))
                $Camera['Camera']['ResolutionX'] = intval($resolutionArr[0]);
            if (isset($resolutionArr[1]))
                $Camera['Camera']['ResolutionY'] = intval($resolutionArr[1]);
            
            $this->request->data = $Camera;
        }
        $this->render('add');
    }

    /**
     * 
     */
    public function delete($id = null) {
        if ($id == null) {
            throw new NotFoundException(__('Invalid Camera'));
        }

        if ($this->Camera->delete($id)) {
            $this->Session->setFlash(__('The Camera with id: %s has been deleted.', h($id)));
            return $this->redirect(array('action' => 'index'));
        }
    }

    /**
     * 
     */
    public function view($id = null, $single = null,$click = 0) {
        $this->layout = false;
        if (!$id) {
            throw new NotFoundException(__('Invalid Camera'));
        }

        $Camera = $this->Camera->find('first', array('conditions' => array('Camera.Id' => $id)));
        
        if (!$Camera) {
            throw new NotFoundException(__('Invalid Camera'));
        }
        $width = "252";
        $height = "170";
        if ($single == 'single') {
            $width = "500";
            $height = "260";
        }
        if ($single == 'big') {
            $width = "500";
            $height = "260";
        }
        if ($single == 'md') {
            $width = "312";
            $height = "160";
        }
        $this->set('camera', $Camera);
        $this->set('width', $width);
        $this->set('height', $height);
        $this->set('click',$click);
    }
    public function multiview($id = null, $single = null,$click = 0){
        $this->layout = false;
        if (empty($_GET['data'])) {
            throw new NotFoundException(__('Invalid Camera'));
        }

        $data= $_GET['data'];
        $data_id =json_decode($data);
        $data_camera= array();
        foreach ($data_id as $value) {
            $data_camera[]= $this->Camera->find('first', array('conditions' => array('Camera.Id' => $value)));
       }
        if (!$data_camera) {
            throw new NotFoundException(__('Invalid Camera'));
        }
        //$width = "252";
        $height = "90%";
        
        $this->set('click',$click);        
        //$this->set('width', $width);
        $this->set('height', $height);
        $this->set('data_camera',$data_camera);
    }

}
